package at.fhv.components.complexe;

import javax.media.jai.KernelJAI;

import at.fhv.components.dilate.DilateFilter;
import at.fhv.components.erode.ErodeFilter;
import at.fhv.components.median.MedianFilter;
import at.fhv.components.roi.ROIFilter;
import at.fhv.components.threshold.ThresholdFilter;
import at.fhv.framework.events.ImageEvent;
import at.fhv.framework.listeners.ImageProcessingListener;
import at.fhv.framework.listeners.RefireListener;

/**
 * The ImagePanel class.
 * 
 * @author Michael Sieber
 */
public class ComplexeFilter implements ImageProcessingListener, RefireListener
{

	private ROIFilter		_roi;
	private ThresholdFilter	_threshold;
	private MedianFilter	_median;
	private ErodeFilter		_erode;
	private DilateFilter	_dilate;

	/**
	 * Create a new ImagePanel
	 */
	public ComplexeFilter()
	{
		// initialize the filters
		_roi = new ROIFilter();
		_threshold = new ThresholdFilter();
		_median = new MedianFilter();
		_erode = new ErodeFilter();
		_dilate = new DilateFilter();

		// set the operating datas
		_roi.setX(0);
		_roi.setY(50);
		_roi.setWidth(448);
		_roi.setHeight(110);
		_threshold.setLow(0d);
		_threshold.setHigh(35d);
		_threshold.setMap(255d);
		_median.setMaskSize(3);
		_erode.setKernel(new KernelJAI(10, 10, new float[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 1, 1 }));
		_dilate.setKernel(new KernelJAI(3, 3, new float[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }));

		// connect the filters
		_roi.addImageChangeListener(_threshold);
		_threshold.addImageChangeListener(_median);
		_median.addImageChangeListener(_erode);
		_erode.addImageChangeListener(_dilate);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void imageChanged(ImageEvent event) {
		_roi.imageChanged(event);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void addRefireListener(RefireListener listener) {
		_roi.addRefireListener(listener);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void removeRefireListener(RefireListener listener) {
		_roi.removeRefireListener(listener);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void refire() {
		_roi.refire();
	}

	/**
	 * Add an image change listener
	 * 
	 * @param listener
	 *            The image change listener to add
	 */
	public void addImageChangeListener(ImageProcessingListener listener) {
		_dilate.addImageChangeListener(listener);
	}

	/**
	 * Remove an image change listener
	 * 
	 * @param listener
	 *            The image change listener to remove
	 */
	public void removeImageChangeListener(ImageProcessingListener listener) {
		_dilate.removeImageChangeListener(listener);
	}

}
